package middle;

public class Solution_130 {

    private int[][] direct = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    private boolean[][] closed;

    public void solve(char[][] board) {
        // 默认false，代表全部都为X
        int row = board.length;
        int col = board[0].length;
        closed = new boolean[row][col];

        for (int j = 0; j < col; j++) {
            if (board[0][j] == 'X') {
                continue;
            }
            dps(board, 0, j);
        }

        for (int j = 0; j < col; j++) {
            if (board[row-1][j] == 'X') {
                continue;
            }
            dps(board, row - 1, j);
        }

        for (int i = 0; i < row; i++) {
            if (board[i][0] == 'X') {
                continue;
            }
            dps(board, i, 0);
        }

        for (int i = 0; i < row; i++) {
            if (board[i][col - 1] == 'X') {
                continue;
            }
            dps(board, i, col - 1);
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == 'O' && !closed[i][j]) {
                    board[i][j] = 'X';
                }
            }
        }
    }

    private void dps(char[][] board, int r, int c) {
        closed[r][c] = true;

        // 默认是封闭的
        for (int[] item : direct) {
            int row = r + item[0];
            int col = c + item[1];
            if (row < 0 || col < 0 || row == board.length || col == board[0].length) {
                continue;
            }
            if (!closed[row][col] && board[row][col] == 'O') {
                dps(board, row, col);
            }
        }
    }

    public static void main(String[] args) {
        Solution_130 model = new Solution_130();
//                char[][] board = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'X', 'O', 'X'}, {'X', 'O', 'X', 'X'}};
        char[][] board = {{'O'}};
        model.solve(board);
        System.out.println();
    }
}
